package com.ruizhong.centercontroll.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruizhong.centercontroll.dao.TAudioMapper;
import com.ruizhong.centercontroll.entity.TAudio;
import com.ruizhong.centercontroll.entity.TCommand;
import com.ruizhong.centercontroll.service.TAudioService;
import com.ruizhong.centercontroll.service.TCommandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TAudioServiceImpl extends ServiceImpl<TAudioMapper, TAudio> implements TAudioService {

    @Autowired
    private TCommandService tCommandService;

    @Override
    public void saveAudioList(List<TAudio> audioList, Integer pageId) {
        deleteAudiosByPageId(pageId);
        final HashMap<Integer, List<Map<String, Object>>> audioIdMapCommandList = new HashMap<>();
        for (TAudio tAudio : audioList) {
            tAudio.setPageId(pageId);
            save(tAudio);
            List<Map<String, Object>> audioCommandList = tAudio.getAudioCommandList();
            audioIdMapCommandList.put(tAudio.getId(), audioCommandList);
        }
        Runnable runnable = new Runnable() {
            public void run() {
                tCommandService.saveCommandList("AUDIO", "audio", audioIdMapCommandList);
            }
        };
        new Thread(runnable).start();
    }

    @Override
    public List<TAudio> getAudioListByPageId(Integer id) {
        List<TAudio> audioList = lambdaQuery()
                .eq(TAudio::getPageId, id)
                .list();
        for (TAudio tAudio : audioList) {
            List<TCommand> audioCommands = tCommandService.getCommandListByMain(tAudio.getId(), tAudio.getType(), "audio");
            audioCommands = tCommandService.fillTCommands(audioCommands);
            List<Map<String, Object>> audioCommandMap = JSON.parseObject(JSON.toJSONString(audioCommands), List.class);
            tAudio.setAudioCommandList(audioCommandMap);
        }
        return audioList;
    }

    @Override
    public void executeCommand(Map<String, Object> conditionMap, String ip) {
        Map<String, Object> variableMap = (Map<String, Object>) conditionMap.get("variableSet");
        Object pageId = conditionMap.get("pageId");
        String wsToken = conditionMap.get("wsToken").toString();
        if (pageId == null) {
            return;
        }
        Object audioContent = conditionMap.get("audioContent");
        if (audioContent == null) {
            return;
        }
        String regex = "[。？！ ]";
// 使用replaceAll方法替换所有匹配正则表达式的部分为空字符串
        String audioContentStr = audioContent.toString().replaceAll(regex, "");
        audioContentStr = audioContentStr.replaceAll("\\[", "").replaceAll("]", "");

        List<TAudio> tAudios = lambdaQuery()
                .eq(TAudio::getPageId, Integer.parseInt(pageId.toString()))
                .list();
        for (TAudio tAudio : tAudios) {
            String commandAudio = tAudio.getCommandAudio();
            if (audioContentStr.contains(commandAudio)) {
                List<TCommand> commandList = tCommandService.getCommandListByMain(tAudio.getId(), tAudio.getType(), "audio");
                tCommandService.executeCommandsAndGetReplyOfAudio(commandList, variableMap,wsToken,ip);
                return;
            }
        }

    }

    private void deleteAudiosByPageId(Integer pageId) {
        QueryWrapper<TAudio> deleteWrapper = new QueryWrapper<>();
        deleteWrapper.lambda().eq(TAudio::getPageId, pageId);
        remove(deleteWrapper);
    }
}
